<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        table{
            border-collapse: collapse;
            margin: 20px auto 0;
        }
        th,td{
            border: 1px solid #000;
            width: 200px;
            height: 40px;
            text-align: center;
        }
        th{
            background-color: #ccc;
        }
        button{
            color: white;
            border: 0;
            background-color: red;
            cursor: pointer;
            border-radius: 3px;
            width: 50px;
            height: 28px;
        }
        input{
            width: 500px;
            margin: 0 auto;
            height: 30px;

        }
        .iptBox{
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="app">
       <div class="iptBox"><input type="text" placeholder="请输入图书名称" @keyup.enter="addBook" v-model="name"></div>
        <table>
            <thead>
                <tr>
                    <th>id</th>
                    <th>书名</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-if="comments.length ==0"><td colspan="4">暂无数据</td></tr>
                <tr v-for="(item,index) of comments" :key="index">
                    <td v-text="index+1"></td>
                    <td v-text="item.name"></td>
                    <td v-text="item.time"></td>
                    <td><button @click="delBook(index)">删除</button></td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script>
    let vm = new Vue({
        el: '#app',
        data() {
            return {
                name:'',
                comments:[
                    
                ]
            }
        },
        methods:{
            addBook(){
               this.comments.push({name:this.name,time:new Date().toLocaleDateString()});
               this.name='';
            },
            delBook(i){
                this.comments.splice(i,1);
            }
        }
    })
</script>
</html>